package com.admin;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.BaseAction;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;




public class LogService extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5335846852356878502L;
	private static String table="\"Loginformation\"";
	private  String Createtime;
	private  int Logtype;
	private int page;
	private int limit;
	private int Logid;
	private  String Url;
	private int Comid;
	private  String Beizhu;
	

	private String Xgzd;
	private String Value;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  void saveLog(Map pharm) {
		
		SqlSession sqlSession = getSqlSession();
		pharm.put("table", table);
		LogMapper log=sqlSession.getMapper(LogMapper.class);
		log.insetLog(pharm);
		sqlSession.close();
		return;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  void getlog() {
		Map pharm=new HashMap();
		SqlSession sqlSession = getSqlSession();
		pharm.put("Createtime", Createtime);
		pharm.put("Logurl", "/dologin");
		pharm.put("table", table);
		LogMapper log=sqlSession.getMapper(LogMapper.class);
		int a=log.selectlog(pharm);
		sqlSession.close();
		jsonWriter(a+"");
		return;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void dotoshowlogtabe() {
		Map pharm=new HashMap();
		int min=(page-1)*limit+1;
		int max=limit*page;
		String tj=" and rownumber between "+min+" and "+max;
		pharm.put("fy", tj);
		
		pharm.put("table", "\"Loginformation\"");
		pharm.put("jointable", "join \"User\" on \"User\".\"Userid\"=\"Loginformation\".\"Createuserid\" " + 
				" join \"Logdictionary\" on \"Logdictionary\".\"Url\"=\"Loginformation\".\"Logurl\""+
				" join \"User_Company\" on \"User_Company\".\"Userid\"=\"User\".\"Userid\" and \"User_Company\".\"Checked\"=1 ");
		pharm.put("nr", "\"Loginformation\".*,to_char(\"Loginformation\".\"Createtime\", 'YYYY-MM-DD HH24:MI:ss') as \"Dotime\",\"User\".\"Username\",\"Logdictionary\".\"Beizhu\",\"User\".\"Usercode\",\"Comid\"");
		pharm.put("px","\"Loginformation\".\"Createtime\" desc ");
		if(Comid!=0) {
			
			pharm.put("where", "\"Comid\"="+Comid);
			
		}else {
			
			pharm.put("where", "1=1");
			
		}
		
		List<LinkedHashMap<String, String>>	list =selecttable(pharm);
		if(list.size()>0) {
			
			String strs = gson.toJson(list.get(0));
			JsonParser parser = new JsonParser();
			JsonElement jsonEl = parser.parse(strs);

			JsonObject jsonObj = null;
			jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
			String number=jsonObj.get("Count").getAsString();//status节点
			ajaxJsontablefy(number,list);	
				
		}else {
			ajaxJsontablefy("0",null);	
				
		}return;
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void dotoshowlogrole() {
		Map pharm=new HashMap();
		pharm.put("where", "1=1");
		pharm.put("nr", "*");
		pharm.put("px","\"Logid\" desc ");
		List<LinkedHashMap<String, String>>	list=findlogdic(pharm);
		if(list.size()>0) {
			
			ajaxJsontable(list);	
				
		}else {
			ajaxJsontable(null);	
				
		}return;
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private List<LinkedHashMap<String, String>>	 findlogdic(Map pharm) {
		
		pharm.put("table", "\"Logdictionary\"");
		List<LinkedHashMap<String, String>>	list =selecttable(pharm);
		
		return list;
		
		
	}
	
	@SuppressWarnings("unchecked")
	public void savelogrole() {
		if(Url==null||Beizhu==null) {
			message="数据丢失！";
			
		}else {
			@SuppressWarnings("rawtypes")
			Map pharm=new HashMap();
			String where="\"Url\"="+"'"+Url+"'";
			pharm.put("where", where);
			pharm.put("px","\"Logid\" desc ");
			pharm.put("nr", "*");
			List<LinkedHashMap<String, String>>	list = findlogdic(pharm);
			if(list.size()>0){//重复
				message="方法名称重复！";
			}else{
				pharm.put("zdm", "\"Url\",\"Beizhu\"");
				pharm.put("nr", "'"+Url+"','"+Beizhu+"'");
				pharm.put("table", "\"Logdictionary\"");
			inserttable(pharm);
			message="1";
			}
			
		}
		
		ajaxJson(message);
		return;
		
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void xglogroled() {
		if(Logid==0) {
			message="-1";
			ajaxJson(message);
			return;
		}
		
		if(Xgzd.equals("Url")) {
			Map pharm=new HashMap();
			String where="\"Url\"="+"'"+Value+"'";
			pharm.put("where", where);
			pharm.put("px","\"Logid\" desc ");
			pharm.put("nr", "*");
			List<LinkedHashMap<String, String>>	list = findlogdic(pharm);
			if(list.size()>0) {
				message="1";
				ajaxJson(message);
				return;
			}
		}
		Map params = new HashMap();
		params.put("Keyidvalue", Logid);
		params.put("Keyid", "Logid");
		params.put("Xgzd",Xgzd);
		params.put("Value",Value);
		params.put("table", "\"Logdictionary\"");
		int result=updatetablebykey(params);
			if(result==0){//修改失败   数据库保存失败
				message="2";
			}
		
		
		ajaxJson(message);
	}
	
	

	public  void getypused() {
		
		String nc=gson.toJson(getDisk());
		jsonWriter(nc);
		return;
	}
	
    public  void getcpuused() {
		
		String cpuused=getCpuRatioForWindows();
		jsonWriter(cpuused);
		return;
	}

	public String getCreatetime() {
		return Createtime;
	}

	public void setCreatetime(String createtime) {
		Createtime = createtime;
	}

	public int getLogtype() {
		return Logtype;
	}

	public void setLogtype(int logtype) {
		Logtype = logtype;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getUrl() {
		return Url;
	}

	public void setUrl(String url) {
		Url = url;
	}

	public String getBeizhu() {
		return Beizhu;
	}

	public void setBeizhu(String beizhu) {
		Beizhu = beizhu;
	}

	public int getLogid() {
		return Logid;
	}

	public void setLogid(int logid) {
		Logid = logid;
	}
	
	public String getXgzd() {
		return Xgzd;
	}

	public void setXgzd(String xgzd) {
		Xgzd = xgzd;
	}

	public String getValue() {
		return Value;
	}

	public void setValue(String value) {
		Value = value;
	}

	public int getComid() {
		return Comid;
	}

	public void setComid(int comid) {
		Comid = comid;
	}
	
	
}
